package org.hashdb.client.config;

import lombok.Data;
import lombok.Getter;
import org.hashdb.client.manager.AppContext;

import java.io.File;
import java.nio.charset.Charset;
import java.util.logging.Level;

/**
 * Date: 2023/12/1 20:11
 *
 * @author huanyuMake-pecdle
 * @version 0.0.1
 */
@Data
public class AppConfig {

    protected static final AppConfig INSTANCE = new AppConfig();

    public static AppConfig getInstance() {
        return INSTANCE;
    }

    private String host = "localhost";

    private int port = 4090;

    private String appName = "HashDB-CLI";
    private boolean prettifyJson = true;

    private Charset outputEncoding = null;

    private int heartbeatInterval = 10_000;

    private int timeoutRetry = 3;

    private Level logLevel = Level.INFO;

    @Getter
    private AppContext appContext;

    public void setAppContext(AppContext appContext) {
        if (this.appContext != null) {
            throw new IllegalStateException();
        }
        this.appContext = appContext;
    }

    protected AppConfig() {
    }

    public AppConfig load(String configFile) {
        File file = new File(configFile);
        if (!file.exists()) {
            System.out.println("config file not found, use default config: " + configFile);
        }
        return this;
    }

    public String getServer() {
        return '\'' + host + ':' + port + '\'';
    }
}
